package vietnam.traveltools.adapter;

import java.util.List;

import vietnam.traveltools.FoodContentActivity;
import vietnam.traveltools.R;
import vietnam.traveltools.TravelContentActivity;
import vietnam.traveltools.variables.ListItem;
import vietnam.traveltools.variables.MySQLiteOpenHelper;
import vietnam.traveltools.view.FoodView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.DataSetObservable;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.SpinnerAdapter;

public class FoodAdapter extends ArrayAdapter<ListItem> implements ListAdapter,
		SpinnerAdapter {
	private final DataSetObservable mDataSetObservable = new DataSetObservable();
	private List<ListItem> _listItems;
	private Context _context;

	public FoodAdapter(Context context, int textViewResourceId,
			List<ListItem> objects) {
		super(context, textViewResourceId, objects);
		this._context = context;
		this._listItems = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final FoodView view = new FoodView(this._context);
		view.setListItem(this._listItems.get(position));

		view.lnFood.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String description = view.tvDescription.getText().toString();
				String tvID = view.tvID.getText().toString();

				if (description.contains("Click để biết")) {
					Bundle bundle = new Bundle();
					bundle.putInt(MySQLiteOpenHelper.cl_id_food,
							Integer.parseInt(tvID));

					Intent intent = new Intent(getContext(),
							FoodContentActivity.class);
					intent.putExtra(MySQLiteOpenHelper.cl_id_food, bundle);

					getContext().startActivity(intent);
					((Activity) getContext()).overridePendingTransition(
							R.anim.right_in, R.anim.left_out);
				}
			}
		});

		return view;
	}

	public void notifyDataSetChanged() {
		mDataSetObservable.notifyChanged();
	}

	public boolean hasStableIds() {
		return false;
	}

	public void registerDataSetObserver(DataSetObserver observer) {
		mDataSetObservable.registerObserver(observer);
	}

	public void unregisterDataSetObserver(DataSetObserver observer) {
		mDataSetObservable.unregisterObserver(observer);
	}

	/**
	 * Notifies the attached observers that the underlying data is no longer
	 * valid or available. Once invoked this adapter is no longer valid and
	 * should not report further data set changes.
	 */
	public void notifyDataSetInvalidated() {
		mDataSetObservable.notifyInvalidated();
	}

	public boolean areAllItemsEnabled() {
		return true;
	}

	public boolean isEnabled(int position) {
		return true;
	}

	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		return getView(position, convertView, parent);
	}

	public int getItemViewType(int position) {
		return 0;
	}

	public int getViewTypeCount() {
		return 1;
	}

	public boolean isEmpty() {
		return getCount() == 0;
	}

}
